<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Series extends Base
{
	//菜单列表
	public function index()
	{

		$data = $this->request->param();
		if (isset($data['ajax']) && $data['ajax'] == 1) {
			$page = isset($data['page']) ? $data['page'] : 1;
			$page = intval($page);
			$limit = isset($data['rows']) ? $data['rows'] : 10;
			$limit = intval($limit);
			$start = $limit * ($page - 1);

			if (isset($data['search'])) {
				if ($data['tj'] == 0) {
					$list = Db::name('series')->where('name', 'like', '%' . $data['search'] . '%')->order('id desc')->limit($start, $limit)->select();
					$count = Db::name('series')->where('name', 'like', '%' . $data['search'] . '%')->count();
				} else {
					$brand_id = Db::name('brand')->where('name', 'like', '%' . $data['search'] . '%')->select();
					$brand_id = array_column($brand_id, 'id');
					$list = Db::name('series')->where('brand_id', 'in', $brand_id)->order('id desc')->limit($start, $limit)->select();
					$count = Db::name('series')->where('brand_id', 'in', $brand_id)->order('id desc')->count();
				}
			} else {
				$list = Db::name('series')->order('id desc')->limit($start, $limit)->select();
				$count = Db::name('series')->count();
			}

			foreach ($list as $key => $value) {
				$brand = Db::name('brand')->where('id', $value['brand_id'])->find();
				$list[$key]['brand'] = $brand['name'];
				$list[$key]['company'] = Db::name('company')->where('id', $brand['company_id'])->value('name');
				$list[$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
			}
			return $this->json_data($count, $list);
		}
		return $this->fetch();
	}

	public function add()
	{
		$brand = Db::name('brand')->select();
		$this->assign('brand', $brand);
		return $this->fetch();
	}

	//编辑栏目1-1
	public function edit($id)
	{
		if (empty($id)) {
			$info = [];
		} else {
			$info = Db::name('series')->where('id', $id)->find();
		}
		$brand = Db::name('brand')->select();
		$this->assign('brand', $brand);
		$this->assign('info', $info);
		return $this->fetch('edit');
	}
	//编辑栏目1-2
	public function save()
	{
		$status = 0;
		$msg = "操作失败";
		$data = $this->request->param();

		if (empty($data['id'])) {
			$update = [
				'name' => $data['name'],
				'brand_id' => $data['brand_id'],
				'status' => 0,
				'addtime' => time(),
				'updatetime' => time(),
			];
			//执行操作
			$res = Db::table('series')->insert($update);

			if ($res) {
				$status = 1;
				$msg = "添加成功";
			}
		} else {
			$id = $data['id'];
			if (empty($data['status'])) {
				$data['status'] = 0;
			}

			$update = [
				'name' => $data['name'],
				'brand_id' => $data['brand_id'],
				'status' => $data['status'],
				'updatetime' => time(),
			];
			//执行更新操作
			$res = Db::table('series')->where('id', $id)->update($update);

			if ($res) {
				$status = 1;
				$msg = "更新成功";
			}
		}

		return ['status' => $status, 'msg' => $msg];
	}

	// 上传资料
	public function uploads_data()
	{
		$data = $this->request->param();
		if (request()->isPost()) {
			// halt($data);
			$data['addtime'] = time();
			$res = Db::table('series_data')->insert($data);
			if (!$res) {
				return json(['status' => 0, 'msg' => '添加失败']);
			}
			return json(['status' => 1, 'msg' => '添加成功']);
		}
		$pid = input('id');
		$this->assign('pid', $pid);
		return $this->fetch();
	}

	//删除栏目1-1
	public function del($id)
	{
		$status = 0;
		$msg = "操作失败";
		if (empty($id)) {
			$msg .= '，ID不存在';
		} else {
			$count = Db::name('good')->where('series', $id)->count();
			if ($count) {
				$msg = "请先删除该系列下的商品";
			} else {
				$del = Db::name('series')->where('id', $id)->delete();
				if ($del) {
					$status = 1;
					$msg = "删除成功";
					adminlog('删除系列id:' . $id, '删除');
				}
			}
		}

		return ['status' => $status, 'msg' => $msg];
	}

	public function daochu()
	{
		$data = $this->request->param();
		if (!empty($data['stime'])) {
			$data['stime'] = strtotime($data['stime'] . " 00:00:00");
			$data['etime'] = strtotime($data['etime'] . " 23:59:59");
			$lists = Db::name('series')->order('id desc')->where('addtime', '>=', $data['stime'])->where('addtime', '<=', $data['etime'])->select();
		} else {
			$lists = Db::name('series')->order('id desc')->select();
		}

		foreach ($lists as $key => $value) {
			$brand = Db::name('brand')->where('id', $value['brand_id'])->find();
			$company = Db::name('company')->where('id', $brand['company_id'])->find()['name'];
			$lists[$key]['brand'] = $brand['name'];
			$lists[$key]['company'] = $company;
			$lists[$key]['addtime'] = $value['addtime'] == null ? '' : date('Y-m-d H:i:s', $value['addtime']);
		}

		$spreadsheet = new Spreadsheet();
		$PHPSheet = $spreadsheet->getActiveSheet();

		$PHPSheet->setTitle("商品类别"); //给当前活动sheet设置名称
		$PHPSheet->setCellValue("A1", "类别名称")
			->setCellValue("B1", "商品品牌")
			->setCellValue("C1", "商家")
			->setCellValue("D1", "添加时间");
		$styleArray = [
			'alignment' => [
				'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
			],
		];
		foreach ($lists as $key => $val) {
			$spreadsheet->setActiveSheetIndex(0)
				->setCellValue("A" . ($key + 2), $val['name'])
				->setCellValue("B" . ($key + 2), $val['brand'])
				->setCellValue("C" . ($key + 2), $val['company'])
				->setCellValue("D" . ($key + 2), $val['addtime']);

			$spreadsheet->setActiveSheetIndex(0)->getStyle('A' . ($key + 2))->applyFromArray($styleArray);
		}

		$spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(30);

		$time = date('Y-m-d', time());
		// Redirect output to a client’s web browser (Xlsx)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="商品类别' . $time . '.xlsx"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		// If you're serving to IE over SSL, then the following may be needed
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
		header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header('Pragma: public'); // HTTP/1.0

		$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
		$writer->save('php://output');
	}
}
